﻿
@{
    Layout = null;
}


<link href="~/libs/layui-v2.5.6/layui/css/layui.css" rel="stylesheet" />

<form class="layui-form" action="">
    <div class="layui-form-item">
        <label class="layui-form-label">退货单编号</label>
        <div class="layui-input-block">
            <input type="text" id="returnCode" name="returnCode" lay-verify="required" placeholder="请输入退货单编号" autocomplete="off" class="layui-input" style="width:200px">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">退货单名称</label>
        <div class="layui-input-block">
            <input type="text" id="returnName" name="returnName" lay-verify="required" placeholder="请输入退货单名称" autocomplete="off" class="layui-input" style="width:200px">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">采购订单编号</label>
        <div class="layui-input-block">
            <input type="text" id="purchaseCode" name="purchaseCode" lay-verify="required" placeholder="请输入采购订单编号" autocomplete="off" class="layui-input" style="width:200px">
        </div>
    </div>
    <div class="layui-form-item">
        <div class="layui-inline">
            <label class="layui-form-label">供应商</label>
            <div class="layui-input-inline">
                <select id="vendorSelect" name="supplierManagementCode">
                    <option value="0">请选择供应商</option>
                </select>
            </div>
        </div>
    </div>
    <div class="layui-form-item">
        <div class="layui-inline">
            <label class="layui-form-label">退货日期</label>
            <div class="layui-input-inline layui-input-wrap">
                <div class="layui-input-prefix">
                    <i class="layui-icon layui-icon-date"></i>
                </div>
                <input type="text" name="returnDate" id="returnDate" lay-verify="date" placeholder="请输入退货日期" autocomplete="off" class="layui-input">
            </div>
        </div>
    </div>
    <div class="layui-form-item">
        <div class="layui-inline">
            <label class="layui-form-label">单据状态</label>
            <div class="layui-input-inline">
                <select id="dstatus" name="dstatus">
                    <option value="">请选择单据状态</option>
                    <option value="1">已完成</option>
                    <option value="0">草稿</option>
                </select>
            </div>
        </div>
    </div>
    <div class="layui-form-item">
        <div class="layui-input-block">
            <button type="submit" class="layui-btn" lay-submit lay-filter="add">保存</button>
            <button type="reset" class="layui-btn layui-btn-primary" onclick="closeWorkStation()">取消</button>
        </div>
    </div>
</form>


<script src="~/libs/jquery/jquery.js"></script>
<script src="~/libs/layui-v2.5.6/layui/layui.js"></script>
<script>
    //关闭弹框
    function closeWorkStation() {
        var index = parent.layer.getFrameIndex(window.name);//先得到当前的索引
        parent.layer.close(index);//再执行关闭
    }
    //获取供应商退货编号
    var number = "TH" + Math.floor(1000 + Math.random() * 9000);
    $("#returnCode").val(number);

    layui.use(['form', 'laydate', 'util'], function () {
        var form = layui.form;
        var layer = layui.layer;
        var laydate = layui.laydate;
        var util = layui.util;

        // 提交事件
        form.on('submit(add)', function (data) {
            var field = data.field; // 获取表单字段值
            $.ajax({
                type: 'post',
                url: '/SupplierReturn/CreateSupplierReturn',
                dataType: 'json',
                data: field,
                success: function (res) {
                    if (res > 0) {
                        layer.msg('添加成功')
                        closeWorkStation()
                        return false;
                    }
                    if (res == -1) {
                        layer.msg('退货单名称已存在')
                        return false;
                    }
                    if (res == -2) {
                        layer.msg('采购订单编码已存在')
                        return false;
                    }
                }
            })
            return false; // 阻止默认 form 跳转
        });

        // 日期
        laydate.render({
            elem: '#returnDate'
        });

        //获取供应商信息
        $(document).ready(function () {
            
            // 使用 AJAX 获取供应商数据  
            $.ajax({
                url: '/SupplierReturn/GetSupplierManagement', // 替换为你的 API 路径  
                type: 'get',
                dataType: 'json',
                async:true,
                success: function (res) {
                    // 清空现有选项  
                    $('#vendorSelect').empty();
                    $('#vendorSelect').append('<option value="0">请选择供应商</option>');
                    console.log(res)
                    // 遍历返回的数据  
                    $.each(res, function (index, vendor) {
                        $('#vendorSelect').append(
                            '<option value="' + vendor.supplierManagementCode + '">' + vendor.supplierName + '</option>' );
                    });
                    // 重新渲染 layui 组件
                    layui.form.render('select');
                },
                error: function (xhr, status, error) {
                    console.error('获取供应商失败:', error);
                }
            });

            // 监听下拉框变化  
            $('#vendorSelect').change(function () {
                var selectedValue = $(this).val();
                console.log("选择的供应商ID:", selectedValue);
                // 根据选择的供应商进行其他操作  
            });
        });  



    });
</script>



